<template>
  <div class="item-wrap">
    <div class="info-wrap" @click="handleShowIpt">
      <div class="title">
        {{ item.ssid }}
      </div>
      <div class="icon-w">
        <img :src="key" alt="icon" v-if="item.pwd">
      </div>
      <div class="wifi-icon">
        <div class="top" :class="getWifiSign"></div><div class="bottom"></div>
      </div>
    </div>
    <div class="enter-wrap" v-if="index === currentIndex">
      <div class="ipt-wrap">
        <input v-model="password" placeholder="请输入密码" v-if="item.pwd" type="password">
      </div>
      <div @click="connect" class="button">Connect</div>
    </div>
  </div>
</template>

<script>
import keyIcon from '../assets/key.svg'
export default {
  name: 'wifi-item',
  // props: ['item','index','currentIndex'],
  props: {
    item: {
      type: Object,
      default: {}
    },
    index: {
      type: Number,
      default: 0,
    },
    currentIndex: {
      type: Number,
      default: -1
    }
  },
  data() {
    return {
      key: keyIcon,
      password: ''
    }
  },
  computed:{
    getWifiSign(){
      const { rssi = 0 } = this.item
      if(rssi >= -30){
        return 'sign5'
      }else if(rssi >= -40){
        return 'sign4'
      }else if(rssi >= -50){
        return 'sign3'
      }else if(rssi >= -60){
        return 'sign2'
      }else if(rssi >= -70){
        return 'sign1'
      }else{
        return 'sign0'
      }
    }
  },
  methods: {
    handleShowIpt(){
      if(this.index !== this.currentIndex){
        this.$emit('changeCurrentIndex',this.index)
      }else {
        this.$emit('changeCurrentIndex',-1)
      }

    },
    connect(e){
      let payload = {
        ssid: this.item.ssid
      }
      if(this.item.pwd){
        if(!this.password){
          console.log('请输入密码')
          return
        }
        payload.password = this.password
      }
      this.$ajax.post('/api/v1/wifi/connect',payload)
      .then(data => {
        console.log(data)
      })
      console.log('=======',payload)
    }
  }
}
</script>

<style scoped>
.info-wrap {
  display: flex;
  height: 50px;
  align-items: center;
  padding: 0 10px;
}
.item-wrap {
  border-bottom: 1px solid #698293;
}
.icon-w {
  width: 20px;
  height: 20px;
  flex: 20px 0 0;
}
.icon-w img{
  width: 100%;
  height: 100%;
}
.title {
  flex: 1;
  white-space:nowrap;
  overflow:hidden;
  text-overflow:ellipsis

}
.wifi-icon {
  flex: 20px 0 0;
  position: relative;
  width: 20px;
  height: 20px;
  margin-left: 10px;
}
.bottom {
  background-image: url("../assets/wifi-poor.svg");
  background-size: 20px;
  width: 20px;
  height: 20px;
  background-position: center;
  background-repeat: no-repeat;
}
.top {
  position: absolute;
  width: 20px;
  height: 20px;
  background-repeat: no-repeat;
  background-position: center left;
  background-size: 20px;
  left: 0;
  top: 0;
  background-image: url("../assets/wifi-full.svg");
}
.sign0 {
  width: 0;
}
.sign1 {
  width: 4px;
}
.sign2 {
  width: 8px;
}
.sign3 {
  width: 12px;
}
.sign4 {
  width: 16px;
}
.sign5 {
  width: 20px;
}
.ipt-wrap {
  box-sizing: border-box;
  padding: 0 20px;
  width: 100%;
}
.ipt-wrap input{
  width: 100%;
  height: 30px;
  line-height: 30px;
  border: 1px solid #5a85a3;
  text-align: center;
  border-radius: 2px;
}
.button {
  margin: 10px auto;
  display: block;
  width: 100px;
  height: 30px;
  border-radius: 20px;
  color: #fff;
  background: #4288be;
  text-align: center;
  line-height: 30px;
}
</style>
